/*
* Copyright (c) 2024 Hunan OpenValley Digital Industry Development Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "slot_info_unittest.h"
#include <iostream>
#include <string>
#include "slot_info/slot_info.h"
#include "partitionslot_manager.h"
#include "ota_event/ota_event.h"

using namespace testing::ext;
using namespace UpdaterUt;
using namespace Updater;
using namespace std;

enum ReturnSetOtaStatus {
    SET_OTA_STATUS_FAILED = -1, // Failed to call SetOTAStatus to set information.
    BOOT_SUCCESS_IN_OTA = 0,    // In the ota stage, startup is complete.
    BOOT_SUCCESS_IN_NORMAL = 1, // In the non-ota stage, startup is complete.
    BOOT_FAILD_IN_OTA = 2,      // In the ota stage, startup is failed.
    BOOT_FAILD_IN_NORMAL = 3,   // In the non-ota stage, startup is failed.
};

namespace UpdaterUt {
void SlotInfoUnitTest::SetUpTestCase(void)
{
    cout << "Updater Unit OTAfunUnitTest Begin!" << endl;
}

void SlotInfoUnitTest::TearDownTestCase(void)
{
    cout << "Updater Unit OTAfunUnitTest End!" << endl;
}

HWTEST_F(SlotInfoUnitTest, slot_info_test_001, TestSize.Level1)
{
    EXPECT_NO_THROW(SetActiveSlot());
}

HWTEST_F(SlotInfoUnitTest, slot_info_test_002, TestSize.Level1)
{
    int32_t checkpoint = 1;
    EXPECT_NO_THROW(SetOTACheckpoint(checkpoint));

    checkpoint = 0;
    EXPECT_NO_THROW(SetOTACheckpoint(checkpoint));

    checkpoint = -1;
    EXPECT_NO_THROW(SetOTACheckpoint(checkpoint));
}

HWTEST_F(SlotInfoUnitTest, slot_info_test_003, TestSize.Level1)
{
    // Simulating OTA phase, boot failed, checkpoint is 1
    OHOS::HDI::Partitionslot::V1_1::PartitionSlotManager psMgr;
    int status = 0;
    int32_t checkpoint = 1;
    int32_t ret = psMgr.SetCheckpoint(checkpoint);
    EXPECT_EQ(ret, 0);
    int expectedResult = BOOT_FAILD_IN_OTA;
    int actualResult = SetOTAStatus(status);
    EXPECT_EQ(actualResult, expectedResult);
}

HWTEST_F(SlotInfoUnitTest, slot_info_test_004, TestSize.Level1)
{
    // Simulation of non-OTA phase, boot failed, checkpoint is 0
    OHOS::HDI::Partitionslot::V1_1::PartitionSlotManager psMgr;
    int status = 0;
    int32_t checkpoint = 0;
    int32_t ret = psMgr.SetCheckpoint(checkpoint);
    EXPECT_EQ(ret, 0);
    int expectedResult = BOOT_FAILD_IN_NORMAL;
    int actualResult = SetOTAStatus(status);
    EXPECT_EQ(actualResult, expectedResult);
}

HWTEST_F(SlotInfoUnitTest, slot_info_test_005, TestSize.Level1)
{
    // Simulated OTA phase, boot successful, checkpoint is 1
    OHOS::HDI::Partitionslot::V1_1::PartitionSlotManager psMgr;
    int status = 1;
    int32_t checkpoint = 1;
    int32_t ret = psMgr.SetCheckpoint(checkpoint);
    EXPECT_EQ(ret, 0);
    int expectedResult = BOOT_SUCCESS_IN_OTA;
    int actualResult = SetOTAStatus(status);
    EXPECT_EQ(actualResult, expectedResult);
}

HWTEST_F(SlotInfoUnitTest, slot_info_test_006, TestSize.Level1)
{
    // Simulated OTA phase, boot successful, checkpoint 0
    OHOS::HDI::Partitionslot::V1_1::PartitionSlotManager psMgr;
    int status = 1;
    int32_t checkpoint = 0;
    int32_t ret = psMgr.SetCheckpoint(checkpoint);
    EXPECT_EQ(ret, 0);
    int expectedResult = BOOT_SUCCESS_IN_NORMAL;
    int actualResult = SetOTAStatus(status);
    EXPECT_EQ(actualResult, expectedResult);
}
}
